#include "modulehplccollector.h"
#include  <QDebug>
#include "protocol/hplc/qinfine/collector/hplcqinfinecollectorprotocol.h"
#include "common/hplcqinfinedefine.h"

ModuleHplcCollector::ModuleHplcCollector(QObject *parent)
    : BaseModule (parent)
{
    m_timer = new QTimer(this);
    m_timer->setInterval(BASE_TICK_MS);
    connect(m_timer, &QTimer::timeout, this, &ModuleHplcCollector::onTimeout);
}

ModuleHplcCollector::~ModuleHplcCollector()
{
    delete  m_timer;
}

bool ModuleHplcCollector::setConfig(int id, QVariant &var)
{

}

bool ModuleHplcCollector::getConfig(int id, QVariant &var)
{

}

bool ModuleHplcCollector::setParam(int id, QVariant &var)
{
    QMutexLocker locker(m_mutex);
    switch (id)
    {
    }
}

bool ModuleHplcCollector::getParam(int id, QVariant &var)
{
    QMutexLocker locker(m_mutex);
    switch (id)
    {
    }
}

bool ModuleHplcCollector::setProtocolCmd(int id, QVariant &var)
{
    QMutexLocker locker(m_mutex);
    return m_protocol->setProtocolCmd(id, var);
}

bool ModuleHplcCollector::getProtocolCmd(int id, QVariant &var)
{
    QMutexLocker locker(m_mutex);
    return m_protocol->getProtocolCmd(id, var);
}

void ModuleHplcCollector::setPort(BasePort *port)
{
    QMutexLocker locker(m_mutex);
    if (m_port)
    {
        m_port->disconnect();
        m_port->deleteLater();
    }
    m_port = port;

    connect(port, &BasePort::receivePackets,
            this, &ModuleHplcCollector::onParse, Qt::QueuedConnection);
    connect(this, &ModuleHplcCollector::requestSend,
            port, &BasePort::onSend, Qt::QueuedConnection);
}

BasePort *ModuleHplcCollector::getPort(void)
{
    QMutexLocker locker(m_mutex);
    return m_port;
}

void ModuleHplcCollector::setProtocol(BaseProtocol *protocol)
{
    QMutexLocker locker(m_mutex);
    if (m_protocol)
    {
        m_protocol->disconnect();
        m_protocol->deleteLater();
    }
    m_protocol = protocol;
    connect(m_protocol, &BaseProtocol::requestSend, this, &ModuleHplcCollector::requestSend);
    connect(m_protocol, &BaseProtocol::parseCompleted, this, &ModuleHplcCollector::parseCompleted);
}

BaseProtocol *ModuleHplcCollector::getProtocol()
{
    QMutexLocker locker(m_mutex);
    return m_protocol;
}

void ModuleHplcCollector::setProtocolAddress(quint8 address)
{
    QMutexLocker locker(m_mutex);
    m_protocol->setConfig(0, address);
}

quint8 ModuleHplcCollector::getProtocolAddress(void)
{
    QMutexLocker locker(m_mutex);
    QVariant var;
    m_protocol->getConfig(0, var);
    return var.toUInt();
}

hplc::CollectorParam *ModuleHplcCollector::getParam()
{
    QMutexLocker locker(m_mutex);
    return &m_param;
}

void ModuleHplcCollector::onTimeout()
{
    QMutexLocker locker(m_mutex);
    hplc::qinfine::HplcQinfineCollectorProtocol *protocol = static_cast<hplc::qinfine::HplcQinfineCollectorProtocol*>(m_protocol);
    protocol->onBaseTickTimeout();
}

void ModuleHplcCollector::onParse(QList<ProtocolPacket> packets)
{
    QMutexLocker locker(m_mutex);
    if (m_enable)
        m_protocol->onWork(packets);
}

void ModuleHplcCollector::onStart()
{
    QMutexLocker locker(m_mutex);
    if (m_enable && m_port->isOpen())
        m_timer->start();
}
